//
//  main.cpp
//  mir6
//
//  Created by 종헌 백 on 12. 2. 14..
//  Copyright (c) 2012년 backssi1@naver.com. All rights reserved.
//
#include <iostream>

using namespace std;

class Node {
    private:
        int key;
        Node* pNext;    
    
    public:
        Node(int nTemp) { key = nTemp; pNext = NULL; };        //pNext = NULL;
        ~Node(){ };
        void setKey (int nTemp){ key = nTemp;};
        void setNext (Node* pTemp) {pNext = pTemp;};
        int getKey() { return key; };
        Node* getNext() { return pNext; };
};

class List{
    private:
        Node* pHead;
    public:
        List();
        ~List();
        void insertNode ( int nTemp );
        void deleteNode ( int nTemp );
        void printOut ();

};

List :: List() 
{
    pHead = NULL;
}

List :: ~List() 
{
    Node* pCur;
    while (pHead) 
    {
        pHead = pHead -> getNext();
        delete pCur;
    }
}

void List :: printOut() 
{
    Node* pCur = pHead;
    while (pCur) 
    {
        cout << pCur -> getKey() << " ";
        pCur = pCur -> getNext();
    }
}

void List :: insertNode (int nTemp ) 
{
    Node* pNew = new Node (nTemp);
    Node* pCur = pHead;
    Node* pPre = NULL;
    
    if ( pHead )
    {
        while ( (pNew -> getKey() > pCur -> getKey() ) && pCur)
        {
            pPre = pCur;
            pCur = pCur -> getNext();
        }
        
        if(pPre == NULL)
        {
            cout << "뻑나요" << endl;
        }
        else
        {
            pPre -> setNext(pNew);
            pNew -> setNext(pCur);
        }
    }
    else
    { 
        pHead = pNew;
    }
}
void List :: deleteNode(int nTemp) 
{
    Node* pCur = pHead;
    Node* pPre = NULL;
    while ( pCur && (pCur -> getKey() != nTemp) ) 
    {
        pPre = pCur;
        pCur = pCur -> getNext();
    }
    
        if(!pCur) 
        {
            return;
        }
        else 
        {
            pPre -> setNext( pCur -> getNext() );
            delete pCur;
        }
}

int main ()

{
    List numTable;
    
    numTable.insertNode(8);
    numTable.insertNode(20);
    numTable.insertNode(15);
    numTable.printOut();
    numTable.deleteNode(20);
    
    return 0;
}